package hupuCBA

import (
	"cd-crawler/types"
	"github.com/PuerkitoBio/goquery"
	"log"
)

const playerUrlPrefix = "https://cba.hupu.com"

// 返回值要有url，解析的方法，需要的值
func ParsePlayerList(params types.ParseParams) types.ParseResult {
	////下载内容
	//resp, err := http.Get(url)
	//if err != nil {
	//	log.Printf("http Get occur error: %s", err)
	//	return types.ParseResult{}
	//}
	//defer resp.Body.Close()
	//
	//if resp.StatusCode != 200 {
	//	log.Printf("status code error: %d %s", resp.StatusCode, resp.Status)
	//}
	//reader, err := tools.DecodeHTMLBody(resp.Body, "")

	parseResult := types.ParseResult{}

	// 解析内容
	// Load the HTML document
	doc, err := goquery.NewDocumentFromReader(params.Reader)
	if err != nil {
		log.Fatal(err)
	}

	// Find the review items
	doc.Find(".players_content_padding table tr:nth-child(n+7) td:nth-child(2) a").Each(func(i int, s *goquery.Selection) {
		// For each item found, get the band and title
		//fmt.Printf("%s\n", s.Text())
		//fmt.Println(s.Attr("href"))
		href, _ := s.Attr("href")
		parseResult.UrlRequest = append(parseResult.UrlRequest, types.UrlRequest{
			Url:      playerUrlPrefix + href,
			ParseFuc: types.ParsePlayerDetail,
		})
	})

	return parseResult
}
